import {
  renderWithQiankun,
  qiankunWindow,
} from "vite-plugin-qiankun/dist/helper";
import { createApp } from "vue";
import "@/styles/index.scss";
import VueApp from "./App.vue";
import router from "./router";
import globalComponents from '@/components';
import ArcoVue, { Notification } from '@garco-design/web-vue';
import "@garco-design/web-vue/dist/arco.min.css";
import ArcoVueIcon from '@garco-design/web-vue/es/icon';
import emitter from "./utils/bus";
import config from "../package.json";

let instance = null;
function render(props = {}) {
  const { container, actions } = props;
  instance = createApp(VueApp);
  instance.use(router);
  instance.use(ArcoVue, {});
  instance.use(ArcoVueIcon);
  instance.use(globalComponents);
  instance.mount(
    container ? container.querySelector(`#${config.name}`) : `#${config.name}`
  );

  //   actions.onGlobalStateChange(({ socketData }) => {
  //     // {app: "cloud_gke_base", content: "CloudAuth租户数据同步完成", type: "success", event: "none"}
  //     // console.log('socketData--',socketData);
  //     const { app, content, type = 'info', event } = socketData;

  //     const notifType = ['info', 'success', 'error', 'waring'].includes(type) ? type : 'info';
  //     // state: 变更后的状态; prev 变更前的状态
  //     if (app === 'ecs' && content) {
  //       // @ts-ignore
  //       Notification[notifType]({ title: '提示', content, duration: 5000 });
  //     }

  //     emitter.emit('onSocket', { event, data: content });
  //     // store.commit('app/SET_SOCKET_DATA', data)
  //   });
}

const qiankunLifeCycle = {
  mount(props) {
    render(props);
  },
  bootstrap() {},
  unmount(props) {
    if (instance) {
      instance.unmount();
      instance._container = null;
      instance = null;
    }
  },
  update() {},
};

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render({});
} else {
  renderWithQiankun(qiankunLifeCycle);
}
